package cn.jdy.payment.admin;

import cn.jdy.common.constant.BankConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.payment.entity.Orders;
import cn.jdy.payment.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Heavens on 2014/5/31.
 */
@Controller
@RequestMapping("/order")
public class OrderCtrl extends BaseCtrl {

	@Autowired
	private OrderService orderService;

	/**
	 * 初始化
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mv = new ModelAndView("/operate/orderList.jsp");
		mv.addObject("type", request.getParameter("b"));
		return mv;
	}

	/**
	 * 查询所有订单
	 */
	@RequestMapping("findOrders")
	@ResponseBody
	public Map<String, Object> findOrders(Orders order, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", this.orderService.queryOrders(order, fromDate, toDate));
			jsonMap.put("iTotalRecords", orderService.count(order));
			jsonMap.put("iTotalDisplayRecords", orderService.count(order));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findOrders", e);
		}
		return jsonMap;
	}

	/**
	 * 下载订单
	 *
	 * @return
	 */
	@RequestMapping("/downExcel")
	public void downExcel(Orders order, String fromDate, String toDate, HttpServletResponse response) {
		List<Orders> list = this.orderService.queryOrders(order, fromDate, toDate);
		for(Orders o : list) {
			String s;
			switch(o.getStatus()) {
				case "1": s = "成功"; break;
				case "2": s = "失败"; break;
				case "3": s = "用户撤销"; break;
				case "4": s = "未支付"; break;
				default: s = ""; break;
			}
			o.setStatus(s);
		}
		String[] titles = new String[]{"购买时间", "姓名", "联系方式", "代理地区", "店面面积", "代理状态", "代理详情", "品类"};
		String[][] fieldNames = new String[][]{{"orderDate"}, {"business.userName"}, {"business.telephone"},
				{"business.province", "business.city", "business.county", "business.address"},
				{"business.acreage"}, {"status"}, {"business.agentDetails"}, {"business.bPinlei.value0"}};
		super.downExcel(list, titles, fieldNames, BankConstant.ORDER_TYPE_TOPUP.equals(order.getOrderType()) ? "充值记录" : "交易记录", response);
	}
}
